package cn.ydt.ydt.community.webapi.controller;

import cn.ydt.ydt.common.resful.JsonPage;
import cn.ydt.ydt.common.resful.JsonResult;
import cn.ydt.ydt.community.service.NoticeService;
import cn.ydt.ydt.pojo.community.dto.NoticeAddDTO;
import cn.ydt.ydt.pojo.community.dto.NoticeDeleteDTO;
import cn.ydt.ydt.pojo.community.dto.NoticeListDTO;
import cn.ydt.ydt.pojo.community.dto.NoticeUpdateDTO;
import cn.ydt.ydt.pojo.community.vo.NoticeAddVO;
import cn.ydt.ydt.pojo.community.vo.NoticeListVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/community/notice")
@Api(tags = "通知功能")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @PostMapping("/add")
    @ApiOperation("新增通知的方法")
    public JsonResult addNotice(@RequestBody NoticeAddDTO noticeAddDTO){
        noticeService.addNotice(noticeAddDTO);

        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改通知的方法")
    public JsonResult updateNotice(NoticeUpdateDTO noticeUpdateDTO){
        noticeService.updateNoticeByNoticeIdAndCommunityId(noticeUpdateDTO);

        return JsonResult.ok();
    }

    @GetMapping("/list1")
    @ApiOperation("查询通知的方法（网格员端）")
    public JsonResult<List<NoticeListVO>> selectNotice(){
        List<NoticeListVO> list = noticeService.selectNoticeByCommunityId();

        return JsonResult.ok(list);
    }

    @GetMapping("/list/user")
    @ApiOperation("查询通知的方法(用户端)")
    public JsonResult<List<NoticeListVO>> selectNoticeToUser(){
        List<NoticeListVO> list = noticeService.selectNoticeByCommunityIdUser();

        return JsonResult.ok(list);
    }

    @PostMapping("/list2")
    @ApiOperation("分页查询通知的方法")
    public JsonResult<JsonPage<NoticeListVO>> selectPageNotice(NoticeListDTO noticeListDTO){
        JsonPage<NoticeListVO> list = noticeService.selectNoticePageByCommunityId(noticeListDTO);

        return JsonResult.ok(list);
    }

    @GetMapping("/delete/{noticeId}")
    @ApiOperation("删除通知的方法")
    public JsonResult deleteNotice(@PathVariable Long noticeId){
        noticeService.deleteNoticeByNoticeIdAndCommunityId(noticeId);
        return JsonResult.ok();
    }

}
